package com.jic.order.impl;

import java.lang.Long;
import java.util.List;

import com.jic.order.entity.OrderSplit;
import com.jic.order.entity.OrderSplitCondition;
import com.jic.order.mapper.OrderSplitConditionMapper;
import com.jic.order.mapper.OrderSplitMapper;
import com.jic.order.manage.request.OrderSplitConditionEditRequest;
import com.jic.order.service.OrderSplitService;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单拆单规则表
 * @author : 
 * @email : 
 * @since : 2020-02-26 10:53:12
 * @version : v1.0.0
 */
@Service
public class OrderSplitServiceImpl implements OrderSplitService{

    @Autowired
    private OrderSplitMapper orderSplitMapper;
    @Autowired
    private OrderSplitConditionMapper orderSplitConditionMapper;

    @Override
    public long editOrderSplit(OrderSplit orderSplit) {
          long flag ;
        if(orderSplit.getId()!=null){//修改
            flag = orderSplitMapper.update(orderSplit);
            //先删除所有匹配条件
            orderSplitConditionMapper.delete(orderSplit.getId());
            //新增匹配条件
            for(OrderSplitConditionEditRequest orderSplitConditionEditRequest:orderSplit.getOrderSplitConditionEditList()){
                OrderSplitCondition orderSplitCondition=new OrderSplitCondition();
                orderSplitCondition.setOrderSplitId(orderSplit.getId());
                orderSplitCondition.setVariableName(orderSplitConditionEditRequest.getVariableName());
                orderSplitCondition.setOperator(orderSplitConditionEditRequest.getOperator());
                orderSplitCondition.setConditionValue(orderSplitConditionEditRequest.getConditionValue());
                orderSplitConditionMapper.insert(orderSplitCondition);
            }
        }else {//新增
            flag = orderSplitMapper.insert(orderSplit);
            //新增匹配条件
            for(OrderSplitConditionEditRequest orderSplitConditionEditRequest:orderSplit.getOrderSplitConditionEditList()){
                OrderSplitCondition orderSplitCondition=new OrderSplitCondition();
                orderSplitCondition.setOrderSplitId(orderSplit.getId());
                orderSplitCondition.setVariableName(orderSplitConditionEditRequest.getVariableName());
                orderSplitCondition.setOperator(orderSplitConditionEditRequest.getOperator());
                orderSplitCondition.setConditionValue(orderSplitConditionEditRequest.getConditionValue());
                orderSplitConditionMapper.insert(orderSplitCondition);
            }
        }

           return flag;
    }
    @Override
    public Boolean editOrderSplitStatus(String ids) {
        String[] arrId=ids.split(",");
        for(int i=0;i<arrId.length;i++){
            orderSplitMapper.editOrderSplitStatus(Long.valueOf(arrId[i]));
        }
         return true;
    }

    @Override
    public Boolean delete(String ids) {
        String[] arrId=ids.split(",");
        for(int i=0;i<arrId.length;i++){
            orderSplitMapper.delete(Long.valueOf(arrId[i]));
        }
        return true;
    }

    @Override
    public OrderSplit queryByPrimaryKey(Long id){
        return orderSplitMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<OrderSplit> query(OrderSplit orderSplit){
        return orderSplitMapper.query(orderSplit);
    }

    @Override
    public PageResult<OrderSplit> queryPage(OrderSplit orderSplit, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<OrderSplit> list = orderSplitMapper.queryPage(startIndex, pageQuery.getLimit(), orderSplit);
        long count = orderSplitMapper.queryPageCount(orderSplit);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}